package com.crocodileQ.blog.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.crocodileQ.blog.domain.BlogImageTag;
import com.crocodileQ.blog.domain.vo.BlogImagesVo;
import com.crocodileQ.blog.mapper.BlogImageTagMapper;
import com.crocodileQ.common.constant.BlogConstants;
import com.crocodileQ.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crocodileQ.blog.mapper.BlogImagesMapper;
import com.crocodileQ.blog.domain.BlogImages;
import com.crocodileQ.blog.service.IBlogImagesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图片Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-06-09
 */
@Service
public class BlogImagesServiceImpl implements IBlogImagesService 
{
    @Autowired
    private BlogImagesMapper blogImagesMapper;

    @Autowired
    private BlogImageTagServiceImpl  blogImageTagService;



    /**
     * 查询图片
     * 
     * @param id 图片主键
     * @return 图片
     */
    @Override
    public BlogImagesVo selectBlogImagesById(Long id)
    {

        //获取标签列表
        BlogImageTag blogImageTag = new BlogImageTag();
        blogImageTag.setImageId(id);

        List<BlogImageTag> blogImageTags = blogImageTagService.selectBlogImageTagList(blogImageTag);
       //stream收集tagId
        List<Long> tagIds = blogImageTags.stream().map(BlogImageTag::getTagId).collect(Collectors.toList());
        BlogImages blogImages = blogImagesMapper.selectBlogImagesById(id);
        BlogImagesVo blogImagesVo = BeanUtil.copyProperties(blogImages, BlogImagesVo.class);
        blogImagesVo.setTagIds((ArrayList<Long>) tagIds);

        return blogImagesVo;
    }

    /**
     * 查询图片列表
     * 
     * @param blogImages 图片
     * @return 图片
     */
    @Override
    public List<BlogImagesVo> selectBlogImagesList(BlogImages blogImages)
    {

        return blogImagesMapper.selectBlogImagesList(blogImages);
    }

    /**
     * 新增图片
     * 
     * @param blogImagesVo 图片
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBlogImages(BlogImagesVo blogImagesVo)
    {

        //拷贝数据
        BlogImages blogImages = new BlogImages();
        BeanUtil.copyProperties(blogImagesVo,blogImages);
        //获取用户id
        blogImages.setUserId(BlogConstants.BLOG_ARTICLE_USER_ID);
        blogImages.setCreateTime(DateUtils.getNowDate());
        blogImages.setUpdateTime(DateUtils.getNowDate());
        blogImages.setIsDeleted(BlogConstants.BLOG_DEL_FALSE);
        blogImages.setPrivacySetting(BlogConstants.BLOG_IMG_VISIBLE_PUBLIC);
        //新增图片数据
        blogImagesMapper.insertBlogImages(blogImages);
        //2、标签信息，批量更新中间表
        ArrayList<BlogImageTag> blogImageTags = getBlogImageTags(blogImagesVo, blogImages);

        return blogImageTagService.BatchInsert(blogImageTags);
    }

    /**
     * 整合标签信息
     * @param blogImagesVo
     * @param blogImages
     * @return
     */
    private static ArrayList<BlogImageTag> getBlogImageTags(BlogImagesVo blogImagesVo, BlogImages blogImages) {
        ArrayList<BlogImageTag> blogImageTags = new ArrayList<>();
        for (Long tagId : blogImagesVo.getTagIds()) {
            BlogImageTag blogImageTag = new BlogImageTag(blogImages.getId(), tagId);
            blogImageTags.add(blogImageTag);
        }
        return blogImageTags;
    }


    /**
     * 修改图片
     * 
     * @param blogImagesVo 图片
     * @return 结果
     */
    @Override
    public int updateBlogImages(BlogImagesVo blogImagesVo)
    {
        blogImagesVo.setUpdateTime(DateUtils.getNowDate());
        //删除图片对应标签信息
        blogImageTagService.deleteBlogImageTagByImageId(blogImagesVo.getId());
        //同步更新标签信息
        ArrayList<BlogImageTag> blogImageTags = getBlogImageTags(blogImagesVo, blogImagesVo);
        blogImageTagService.BatchInsert(blogImageTags);

        return blogImagesMapper.updateBlogImages(blogImagesVo);
    }

    /**
     * 批量删除图片
     * 
     * @param ids 需要删除的图片主键
     * @return 结果
     */
    @Override
    public int deleteBlogImagesByIds(Long[] ids)
    {
        //查询是否有图片关联标签
        Integer count  = blogImageTagService.countByImageIds(ids);
        if (count > 0){
            //如果有关联标签，则删除关联标签
            blogImageTagService.deleteBlogImageTagByImageIds(ids);
        }

        return blogImagesMapper.deleteBlogImagesByIds(ids);
    }

    /**
     * 删除图片信息
     * 
     * @param id 图片主键
     * @return 结果
     */
    @Override
    public int deleteBlogImagesById(Long id)
    {
        return blogImagesMapper.deleteBlogImagesById(id);
    }
}
